package com.boot.springboot.controller;

import com.boot.springboot.pojo.ResponseMessage;
import com.boot.springboot.pojo.Staff;
import com.boot.springboot.pojo.dto.StaffDto;
import com.boot.springboot.service.IStaffService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //接口方法返回对象，装换成json文本
@RequestMapping("/staff") //localhost:8080/staff/**
public class StaffController {
    @Autowired //自动装配
    IStaffService staffService;
//通过Rest在http协议中实现

    //增加
    @PostMapping //前端请求地址localhost:8080/staff method:post
    public ResponseMessage<Staff> add(@Validated @RequestBody StaffDto staff){
        Staff staffNew=staffService.add(staff);
        return ResponseMessage.success(staffNew);
    }
    //查询
    @GetMapping("/{staffId}")
    public ResponseMessage<Staff> get(@PathVariable Integer staffId){
        Staff staffNew=staffService.get(staffId);
        return ResponseMessage.success(staffNew);
    }


    //修改
    @PutMapping
    public ResponseMessage<Staff> edit(@Validated @RequestBody StaffDto staff){
        Staff staffNew=staffService.edit(staff);
        return ResponseMessage.success(staffNew);
    }

    //删除用户
    @DeleteMapping("/{staffId}")
    public ResponseMessage<Staff> delete(@PathVariable Integer staffId){
        staffService.delete(staffId);
        return ResponseMessage.delete();
    }
    //查询所有数据
    @GetMapping
    public ResponseMessage<List<Staff>> getAllStaffs() {
        List<Staff> staffs = staffService.getAllStaffs();
        return ResponseMessage.success(staffs);
    }
}
